package com.cskaoyan.controller.back;

import com.cskaoyan.bean.po.BasePageInfo;
import com.cskaoyan.bean.po.CommonData;
import com.cskaoyan.bean.po.MarketTopic;
import com.cskaoyan.bean.vo.back.BaseRespVo;
import com.cskaoyan.bean.vo.back.TopicBatchDeleteVO;
import com.cskaoyan.bean.vo.back.TopicCreateVO;
import com.cskaoyan.bean.vo.back.TopicReadVO;
import com.cskaoyan.service.back.TopicService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 12-江涛
 * @since 2022/10/13  9:02
 */
@RestController
@RequestMapping("admin")
public class TopicController {

    @Autowired
    TopicService topicService;


    /*
     * topic 列表
     * http://182.92.235.201:8083/admin/topic/list
     *
     * @return
     * @Author 12-江涛
     * @Date  2022/10/13 9:06
    */
    @RequiresPermissions("admin:topic:list")
    @RequestMapping("topic/list")
    public BaseRespVo list(BasePageInfo basePageInfo,String title,String subtitle){

        CommonData<MarketTopic> commonDataVO = topicService.topicList(basePageInfo,title,subtitle);

        return BaseRespVo.ok(commonDataVO);
    }

    /*
     * 添加专题
     * http://182.92.235.201:8083/admin/topic/create
     *
     * @return
     * @Author 12-江涛
     * @Date  2022/10/13 9:06
     */
    @RequiresPermissions("admin:topic:create")
    @RequestMapping("topic/create")
    public BaseRespVo create(@RequestBody MarketTopic marketTopic){

        TopicCreateVO topicCreateVO = topicService.topicCreate(marketTopic);

        if (topicCreateVO == null) {

            return BaseRespVo.ok("添加失败");
        }
        return BaseRespVo.ok(topicCreateVO);

    }

    /*
     * 编辑专题
     * http://182.92.235.201:8083/admin/topic/update
     *
     * @return
     * @Author 12-江涛
     * @Date  2022/10/13 9:06
     */
    @RequiresPermissions("admin:topic:update")
    @RequestMapping("topic/update")
    public BaseRespVo update(@RequestBody MarketTopic marketTopic){

       int count = topicService.topicUpdate(marketTopic);
        if (count == 1) {

            return BaseRespVo.ok(marketTopic);
        }
        return BaseRespVo.ok("编辑失败");
    }


    /*
     * 获得专题信息（与update一同触发）
     * http://182.92.235.201:8083/admin/topic/read
     *
     * @return
     * @Author 12-江涛
     * @Date  2022/10/13 9:06
     */
    @RequiresPermissions("admin:topic:read")
    @GetMapping("topic/read")
    public BaseRespVo read(String id){

        int idInt = Integer.parseInt(id);
        TopicReadVO topicReadVO = topicService.topicRead(idInt);

        return BaseRespVo.ok(topicReadVO);

    }

    /*
     * 删除专题
     * http://182.92.235.201:8083/admin/topic/delete
     *
     * @return
     * @Author 12-江涛
     * @Date  2022/10/13 9:06
     */
    @RequiresPermissions("admin:topic:delete")
    @RequestMapping("topic/delete")
    public BaseRespVo delete(@RequestBody MarketTopic marketTopic){

        int count = topicService.topicDelete(marketTopic);
        if (count == 1) {

            return BaseRespVo.ok();
        }
        return BaseRespVo.ok("删除失败");
    }


    /*
     * 批量删除专题
     * http://182.92.235.201:8083/admin/topic/batch-delete
     *
     * @return
     * @Author 12-江涛
     * @Date  2022/10/13 9:06
     */
    @PostMapping("topic/batch-delete")
    public BaseRespVo batchDelete(@RequestBody TopicBatchDeleteVO topicBatchDeleteVO){

        int count = topicService.batchDelete(topicBatchDeleteVO);

        if (count == 1) {
            return BaseRespVo.ok();
        }else {
            return BaseRespVo.isParameter("操作有误");
        }

    }

}
